﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DocumentManagement.ViewsModels;
using DocumentManagement.Models;
using Microsoft.Win32;
using System.IO;

namespace DocumentManagement.Views
{
    /// <summary>
    /// Interaction logic for ShowItemFileIn.xaml
    /// </summary>
    public partial class ShowItemFileIn : Window
    {
        ShowItemFileInViewsModel _showItemFileInViewsModel;
        public ShowItemFileIn(Receive receiveAttach)
        {
            InitializeComponent();
            _showItemFileInViewsModel = this.Resources["showItemFileInViewsModel"] as ShowItemFileInViewsModel;
            _showItemFileInViewsModel.MyReceive = receiveAttach;

            dataGrid1.ItemsSource = receiveAttach.ReceiveAttachedFiles.Where(p => p.Active == true).ToList();
        }

        private void DG_Hyperlink_Click(object sender, RoutedEventArgs e)
        {

            TextBlock textblock = sender as TextBlock;
            var receive = textblock.DataContext as ReceiveAttachedFile;

            string fileName = "D:\\uploadReceive\\";
            SaveFileDialog save = new SaveFileDialog(); 
            save.FileName = receive.FileName;
            save.Filter = "Image files (*.jpg)|*.jpg|Acrobat (*.pdf)| *.pdf|All files (*.*)|*.*";
            var result = save.ShowDialog(); // showdialog มันจะ return true กับ false
            if (result != null)
            {
                if (File.Exists(fileName + receive.FileName))
                    File.Copy(fileName + receive.FileName, save.FileName, true);
                else
                    MessageBox.Show("File not found");
            }

           
        }

      

        //private void DownloadButton_Click(object sender, RoutedEventArgs e)
        //{
        //    Button button = sender as Button;
        //    ReceiveAttachedFile receive = button.DataContext as ReceiveAttachedFile;
            
        //    string fileName = "D:\\upload\\";
        //    SaveFileDialog save = new SaveFileDialog();
        //    save.FileName = receive.FileName;
        //    save.Filter = "Image files (*.jpg)|*.jpg|Acrobat (*.pdf)| *.pdf|All files (*.*)|*.*";
        //    save.ShowDialog();

        //    File.Copy(fileName + receive.FileName, save.FileName, true);
        //}
    }
}
